package com.common.handler;

import com.common.domain.pojo.Result;
import com.common.exception.GlobalException;
import com.common.exception.course.CourseException;
import com.common.exception.media.MediaException;
import com.common.exception.media.MediaFileUploadException;
import com.common.exception.order.OrderException;
import com.common.exception.users.LoginException;
import com.common.exception.users.SmsException;
import com.common.exception.users.UsernamePasswordException;
import com.common.exception.users.UsersException;
import feign.FeignException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


/**
 * 全局异常处理器
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler implements InitializingBean {

//    @Resource
//    EmailTool emailTool;

    @Override
    public void afterPropertiesSet() throws Exception {
        log.debug("全局异常处理器 装配完毕");
    }


    @ExceptionHandler(MediaException.class)
    public Result<String> mediaException(MediaException e) {
        e.printStackTrace();

        log.error("媒资服务 ==》媒体异常 : 异常信息：{}", e.getMessage());
        return Result.error(e.getMessage());
    }

    @ExceptionHandler(MediaFileUploadException.class)
    public Result<String> mediaFileUploadException(MediaFileUploadException e) {
        e.printStackTrace();

        log.error("媒资服务 ==》媒体文件上传异常 : 异常信息：{}", e.getMessage());
        return Result.error(e.getMessage());
    }

    @ExceptionHandler(CourseException.class)
    public Result<String> courseException(CourseException e) {
        e.printStackTrace();

        log.error("课程服务 ==》课程异常 : 异常信息：{}", e.getMessage());
        return Result.error(e.getMessage());
    }

    @ExceptionHandler(LoginException.class)
    public Result<String> loginException(LoginException e) {
        e.printStackTrace();
        log.error("用户服务 ==》登入异常 : 异常信息：{}", e.getMessage());
        return Result.error(e.getMessage());
    }

    @ExceptionHandler(UsernamePasswordException.class)
    public Result<String> usernamePasswordException(UsernamePasswordException e) {
        e.printStackTrace();
        log.error("用户服务 ==》账号密码异常 : 异常信息：{}", e.getMessage());
        return Result.error(e.getMessage());
    }

    @ExceptionHandler(SmsException.class)
    public Result<String> smsException(SmsException e) {
        e.printStackTrace();

        log.error("用户服务 ==》短信异常 : 异常信息：{}", e.getMessage());
        return Result.error(e.getMessage());
    }

    @ExceptionHandler(UsersException.class)
    public Result<String> userException(UsersException e) {
        e.printStackTrace();

        log.error("用户服务 ==》用户异常 : 异常信息：{}", e.getMessage());
        return Result.error(e.getMessage());
    }

    @ExceptionHandler(OrderException.class)
    public Result<String> orderException(OrderException e) {
        e.printStackTrace();

        log.error("订单服务 ==》订单异常 : 异常信息：{}", e.getMessage());
        return Result.error(e.getMessage());
    }

    @ExceptionHandler(FeignException.class)
    public Result<String> FeignException(FeignException e) {
        e.printStackTrace();
        log.error("Feign异常 ==》异常信息：{}", e.getMessage());
        return Result.error(e.getMessage());
    }

    @ExceptionHandler(FeignException.Unauthorized.class)
    public Result<String> FeignExceptionUnauthorized(FeignException.Unauthorized e) {
        e.printStackTrace();
        log.error("Feign异常 ==》异常信息：{}", e.getMessage());
        return Result.unauthorized(e.getMessage());
    }

    @ExceptionHandler(FeignException.Forbidden.class)
    public Result<String> handleFeignForbiddenException(FeignException.Forbidden e) {
        e.printStackTrace();
        log.error("Feign Forbidden 异常 ==》异常信息：{}", e.getMessage());
        return Result.error("权限不足");
    }

    @ExceptionHandler(GlobalException.class)
    public Result<String> GlobalException(GlobalException e) {
        e.printStackTrace();
        log.error("全局异常 ==》异常信息：{}", e.getMessage());
        return Result.error(GlobalException.DEFAULT_MESSAGE);
    }

    @ExceptionHandler(value = IllegalArgumentException.class)
    public Result<String> IllegalArgumentException(IllegalArgumentException e) {
        e.printStackTrace();
        log.error("参数异常 ==》异常信息：{}", e.getMessage());
        return Result.error(e.getMessage());
    }

    public String getStackTraceAsMessage(Exception e){
        StringBuilder sb = new StringBuilder();
        for (StackTraceElement element : e.getStackTrace()) {
            sb.append(element.toString()).append("\n");
        }
        return "无敌的帅帅杰 程序报错了 快解决：\n" +
                "\n" + e.getLocalizedMessage() + "\n" +
                sb;
    }
}
